﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mobile.Models;

namespace Mobile.Controllers
{
    public class AdminUIController : Controller
    {
        //private List<MainService.QueueData> Queue = new List<MainService.QueueData>();
        private Queue Queue = new Queue();
        private int count = 0;


        //
        // GET: /AdminUI./

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Admin is given ability to also add a song from database
        /// </summary>
        /// <param name="partyID"></param>
        /// <returns></returns>
        public ActionResult AddSong(string partyID)
        {
            if (partyID == null || partyID.Equals(""))
            {
                partyID = "fake_party_id";
            }
            MainService.Service s = new MainService.Service();
            List<MainService.SongData> songlist = s.GetAllSongs(partyID).ToList();
            Party party = new Party { PartyID = partyID, allSongs = songlist };

            return View(party);
          
        }

        /// <summary>
        /// Returns Party Queue
        /// </summary>
        /// <param name="partyID"></param>
        /// <returns></returns>
        public PartialViewResult GetQueue(string partyID)
        {
            if (partyID == null) partyID = "123";
            MainService.Service s = new MainService.Service();
            List<MainService.QueueData> QueueDataList = s.GetQueue(partyID).ToList();

            List<Song> songlist = new List<Song>();

            foreach (MainService.QueueData qd in QueueDataList)
            {
                songlist.Add(new Song { ID = qd.Song.SongID, Title = qd.Song.Title, Artist = qd.Song.Artist, SongData = qd });
            }
            if (songlist.Count == 0) { return PartialView(); }
            Song nowplaying = songlist.ElementAt(0);
            songlist.RemoveAt(0);
            var queue = new Queue { partyID = partyID, queue = songlist, nowPlaying = nowplaying };

            return PartialView("_Playlist", queue);
        }

        /// <summary>
        /// Admin has ability to skip a song
        /// </summary>
        /// <param name="partyID"></param>
        /// <returns></returns>
        public ActionResult SkipSong(string partyID)
        {
            //insert logic for skipping song - need to know how to access desktop's music player
            if (partyID == null) partyID = "123";

            MainService.Service s = new MainService.Service();
            List<MainService.QueueData> QueueDataList = s.GetQueue(partyID).ToList();
            List<Song> songlist = new List<Song>();
            foreach (MainService.QueueData qd in QueueDataList)
            {
                songlist.Add(new Song { ID = qd.Song.SongID, Title = qd.Song.Title, Artist = qd.Song.Artist, SongData = qd });
            }
            if (songlist.Count == 0) { return PartialView(); }
            Song skipped = songlist.ElementAt(0);
            songlist.RemoveAt(0);
            s.RemoveSong(partyID, skipped.ID);
            Song nowplaying = songlist.ElementAt(1);
            songlist.RemoveAt(1);
            var queue = new Queue { partyID = partyID, queue = songlist, nowPlaying = nowplaying };

            return PartialView("_Playlist", queue);
        }

        /// <summary>
        /// This Method returns the party's playlist
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public ActionResult Playlist(FormCollection collection)
        {
            string partyCode = collection.Get("partyCode");
            string partyID = null;
            if (partyCode != null)
            {
                partyID = null;
            }
            if (partyID == null)
            {
                //default partyID to our test party in db -- this is for testing
                partyID = "fake_party_id";
            }
            MainService.Service s = new MainService.Service();
            List<MainService.QueueData> QueueDataList = s.GetQueue(partyID).ToList();

            List<Song> songlist = new List<Song>();

            foreach (MainService.QueueData qd in QueueDataList)
            {
                songlist.Add(new Song { ID = qd.Song.SongID, Title = qd.Song.Title, Artist = qd.Song.Artist, SongData = qd });
            }
            
            var queue = new Queue { partyID = partyID, queue = songlist, nowPlaying = songlist.ElementAt(0) };

            return View(queue);
        }

        /// <summary>
        /// Admin will get the upvote option as well
        /// </summary>
        /// <param name="songID"></param>
        /// <param name="partyID"></param>
        /// <returns></returns>
        public ActionResult UpvoteButton(string songID, string partyID)
        {
            if (partyID == null) partyID = "fake_party_id";


            MainService.Service s = new MainService.Service();
            if (partyID != null && songID != null) { s.UpvoteSong(partyID, songID); }

            List<MainService.QueueData> QueueDataList = s.GetQueue(partyID).ToList();
            List<Song> songlist = new List<Song>();

            foreach (MainService.QueueData qd in QueueDataList)
            {
                songlist.Add(new Song { ID = qd.Song.SongID, Title = qd.Song.Title, Artist = qd.Song.Artist, SongData = qd });
            }
            if (songlist.Count == 0) { return PartialView(); }
            Song nowplaying = songlist.ElementAt(0);
            songlist.RemoveAt(0);
            var queue = new Queue { partyID = partyID, queue = songlist, nowPlaying = nowplaying };

            return View("Playlist", queue);
        }

    }
}
